package interview4;

/**
 * @author Jack
 * @date 2019-10-30-15:29
 * 面试题4：寻找有序二维数组中的其中某一个数
 */
public class Solution {

    public static void main(String[] args) {
        int[][] nums = new int[][]{
                {1,2,8,9},
                {2,4,9,12},
                {4,7,10,13},
                {6,8,11,15}
        };
        int target = 11;
        if(isExistNum(nums, target)){
            System.out.println("存在这个数");
        }else{
            System.out.println("不存在这个数");
        }
    }

    public static boolean isExistNum(int[][] nums, int target){
        if(nums == null || nums.length == 0){
            return false;
        }
        int row = 0;
        int column = nums[0].length - 1;
        while(row < nums.length || (column >= 0 && column < nums[0].length)){
            System.out.printf("目前处在的位置是---row:{%d},column:{%d}",row,column);
            System.out.println();
            if(nums[row][column] == target){
                return true;
            }
            if(nums[row][column] < target){
                row++;
                continue;
            }
            if(nums[row][column] > target){
                column--;
                row++;
            }
        }
        return false;
    }

}
